let that,
  api = new(require('../../utils/api.js')),
  util = new(require('../../utils/util.js')),
  //imghead = api.getimgurl(),
  imghead = 'https://www.jihemu.com/bookapi/imgs/',
  videohead='https://www.jihemu.com/bookapi/myvideo/',
  audiohead='https://www.jihemu.com/bookapi/myaudio/',
  AudioContext,
  app = getApp();

// pages/sampleReels/sampleReels.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pagesize:20,
    start:1,
    list:[], audioid:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that=this
    let type=0
    if(options.type){
      type=options.type
    }
    
    AudioContext = wx.createInnerAudioContext()
    AudioContext.onPlay(()=>{
      console.log('正在播放语音')
    })
    AudioContext.onPause(()=>{
      console.log('正在播放暂停')
    })
    AudioContext.onStop(()=>{
      console.log('语音播放停止')
    })
    AudioContext.onEnded(()=>{
      
      console.log('语音播放结束')
    })
    AudioContext.onError(res=>{
      console.log(res,'AudioContexterr');
      
    })
    that.setData({AudioContext})
    that.setData({type},()=>{
      that.getdata()
    })
  },
  getdata(){
    wx.showLoading({
      title: '加载中...',
    })
    let {pagesize,start,list,type}=that.data
    if(type>0){
      api.getMyWorkscomment({pagesize,start},res=>{
        wx.hideLoading()
        if(res.data.issuccess==1){
          if(res.data.list.length>0){
            res.data.list.forEach(x=>{
              x['date'] = util.formatTime(new Date(x.updateDate*1000))
              x.isplay=!1
              list.push(x)
            })
          }
          that.setData({
            list,
            reachbtm: list.length == res.data.total,
            total: res.data.total
          })
        }
      })
    }else{
      api.getEvaluatingList({pagesize,start},res=>{
        wx.hideLoading()
        if(res.data.issuccess==1){
          if(res.data.list.length>0){
            res.data.list.forEach(x=>{
              x['date'] = util.formatTime(new Date(x.updateDate*1000))
              x.isplay=!1
              list.push(x)
            })
          }
          
          that.setData({
            list,
            reachbtm: list.length == res.data.total,
            total: res.data.total
          })
        }
      })
    }
  },
  todetail(e){
    let {cid,cuserid,id}=e.currentTarget.dataset
    wx.navigateTo({
      url: '../sampleReelsDetail/sampleReelsDetail?cid='+cid+'&cuserid='+cuserid+'&id='+id,
    })
  },
  readvoice(e){
    let{isplay,id,src}=e.currentTarget.dataset,{list,audioid}=that.data
    console.log(id,'ididid');
    AudioContext.src = src;
    AudioContext.play()
    if(audioid==id){
      console.log('onStoponStoponStop');
      that.data.AudioContext.pause();
      list.forEach(x=>{
        x.isplay=!1
    })
    that.setData({list,audioid:0})
    }else{
      console.log(22222);
      audioid=id
      that.data.AudioContext.play()
      list.forEach(x=>{
        if(id==x.data.id){
            x.isplay=!0
          }else{
            x.isplay=!1
          }
        })
        that.setData({list,audioid})
    }
    AudioContext.onError((res) => {
      wx.showToast({
        title: '播放失败',
        icon:'none'
      })
      list.forEach(x=>{
        x.isplay=!1
      })
      that.setData({list,audioid:0})
      console.log(res.errMsg)
      console.log(res.errCode)
    })
    AudioContext.onEnded(()=>{
      console.log('语音自然播放结束');
      list.forEach(x=>{
        x.isplay=!1
      })
      that.setData({list,audioid:0})
    })
  },
  thumbsup(e){
    let {id,islike} = e.currentTarget.dataset,{list}=that.data
    if(islike==0){
      api.setlike(id,res=>{
        if(res.data.issuccess==1){
          wx.showToast({
            title: '点赞成功',
            icon:'none',
            duration:2000,
            success(res){
                list.forEach(x=>{
                  if(x.id==id){
                    x.islike=1
                  }
                })
                that.setData({list})
            }
          })
          
        }
      })
    }else{
      api.setlike(id,res=>{
        if(res.data.issuccess==1){
          wx.showToast({
            title: '取消成功',
            icon:'none',
            duration:2000,
            success(res){
              list.forEach(x=>{
                if(x.id==id){
                  x.islike=0
                }
              })
              that.setData({list})
            }
          })
          
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    AudioContext.destroy()
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    AudioContext.destroy()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    let {
      start, total
    } = this.data;
    if (start>total) {
      that.setData({ reachbtm:true})
      wx.showToast({
        icon: "none",
        title: '再怎么扯也没有了',
      })
      return
    }
    that.setData({
      start: that.data.start + that.data.pagesize
    }, () => {
      that.getdata()
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})